<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>this指向问题</title>
</head>
<body>
    <button>点击</button>
    <script>
        //  1. 全局作用域或者函数中this指向window
        console.log(this);
        function fn() {
            console.log(this);
        }
        window.fn();
        window.setTimeout(function() {
            console.log(this);
        },1000)
        //  2.方法调用中谁调用this指向谁
        var o = {
            sayHi:function() {
                console.log(this);//this -> o
            }
        }
        o.sayHi();
        var btn = document.querySelector('button')
        btn.onclick = function() {
            console.log(this);//this -> btn
        }
        btn.addEventListener('click',function(){
            console.log(this);
        })

        // 3.构造函数中this 指向构造函数的实例
        function Fun() {
            console.log(this);//  fun -> 实例对象
        }
        var fun = new Fun()
    </script>
</body>
</html>